package com.itheima.factory;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    static Properties pro = new Properties();
    //创建Map集合存放bean对象
    private static Map<String, Object> map = new HashMap<String, Object>();

    static {
        try {
            //读取配置文件信息
            InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("beans.properties");
            pro.load(is);
            //Enumeration:这个枚举类型的接口不能加泛型
            Enumeration keys = pro.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                //获取值
                String val = pro.getProperty(key);
                //通过反射的方式获取对象
                Object obj = Class.forName(val).newInstance();
                //把创建的每一个对象给存到一个指定的容器
                map.put(key, obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String beanName) throws Exception {
       /* String name = pro.getProperty(beanName);
        Object obj = Class.forName(name).newInstance();*/
        return map.get(beanName);
    }

  /*  public static UserService getUserService() throws Exception {
        String beanName = pro.getProperty("userService");
        pro.getProperty("userService");
        UserService userService = (UserService) Class.forName(beanName).newInstance();
        return userService;
    }

    public static RoleService getRoleService() throws Exception {
        String beanName = pro.getProperty("roleService");
        pro.getProperty("roleService");
        RoleService roleService = (RoleService) Class.forName(beanName).newInstance();
        return roleService;
    }*/


}
